package com.xxx.car.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxx.car.entity.TbBanner;
import com.xxx.car.entity.vo.request.ReqBannerAdminQuery;
import com.xxx.car.mapper.ITbMapper;
import com.xxx.car.mapper.TbBannerMapper;
import com.xxx.car.service.BannerService;
import com.xxx.common.base.vo.APIResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by yxj on 17/6/19.
 */
@Service
public class BannerServiceImpl implements BannerService {
	@Autowired
	TbBannerMapper tbBannerMapper;
	@Autowired
	ITbMapper iTbMapper;
	@Override
	public APIResponse queryBannerListByApp() {
		List<TbBanner> tbBannerList = iTbMapper.queryBannerListByApp();
		return APIResponse.returnSuccess(tbBannerList);
	}

	@Override
	public PageInfo<TbBanner> queryBannerListByAdmin(ReqBannerAdminQuery query) {
		PageHelper.startPage(query.getPageNum(), query.getPageSize());
		List<TbBanner> tbBannerList= iTbMapper.queryBannerListByAdmin();
		return new PageInfo<>(tbBannerList);
	}

	@Override
	public APIResponse addBanner(TbBanner tbBanner) {
		if (tbBanner == null){
			return APIResponse.returnFail("添加失败");
		}
		int result = tbBannerMapper.insertSelective(tbBanner);
		if (result != 1){
			return APIResponse.returnFail("请重试");
		}
		return APIResponse.returnSuccess();
	}

	@Override
	public TbBanner queryBannerDetailById(Integer id) {
		return tbBannerMapper.selectByPrimaryKey(id);
	}

	@Override
	public APIResponse updateBanner(TbBanner tbBanner) {
		if (tbBanner == null){
			return APIResponse.returnFail("编辑失败");
		}
		int result = tbBannerMapper.updateByPrimaryKeySelective(tbBanner);
		if (result != 1){
			return APIResponse.returnFail("请重试");
		}
		return APIResponse.returnSuccess();
	}
}
